<?php
defined('BASEPATH') OR exit('No direct script access allowed');
header("Content-type: text/html; charset=utf-8");
class Api extends CI_Controller{
    var $tk_user;
    function __construct(){
        parent::__construct();
        $token = get_parameter('token', '', 'get');
        if ($token == '') {
            $ret = array( 'status' => 0, 'info' => 'token 验证失败', 'article_id' => 0 );
            json_out($ret);
            exit();
        }
        $this->tk_user = token_user_get_tk($token);
        if (! $this->tk_user) {
            $ret = array( 'status' => 0, 'info' => 'token 验证失败', 'article_id' => 0 );
            json_out($ret);
            exit();
        }
    }
    function add(){
        $ret = array( 'status' => 0, 'info' => 'Error', 'article_id' => 0 );
        $this->form_validation->set_rules ( 'title', '文章标题', 'trim|required|xss_clean');
        $this->form_validation->set_rules ( 'cid', '文章分类', 'trim|required|xss_clean');
        $this->form_validation->set_rules ( 'content', '文章简介', 'trim|required|xss_clean');
        $this->form_validation->set_rules ( 'author', '文章作者', 'trim|required|xss_clean');

        $this->form_validation->set_rules ( 'code', '文章作者', 'trim|xss_clean');
        $this->form_validation->set_rules ( 'author_wx', '作者微信', 'trim|xss_clean');
        $this->form_validation->set_rules ( 'tid', '文章来源类型', 'trim|xss_clean');
        $this->form_validation->set_rules ( 'url', '文章链接', 'trim|xss_clean');
        $x_cid = [1 => 8, 2 => 9, 3 => 10, 4 => 11];
        $xcid_get = get_parameter('xcid', 0);
        $xcid = 0;
        if (in_array($xcid_get, $x_cid)) {
            $xcid = $x_cid[$xcid_get];
        }
        if( $this->form_validation->run() ) {
            $up_data = array(
                'cid' => 1,
                'xcid' => $xcid,
                'tid' => get_parameter('tid'),
                'title' => get_parameter('title', ''),
                'content' => get_parameter('content', ''),
                'code' => get_parameter('code', ''),
                'url' => get_parameter('url', ''),
                'author' => get_parameter('author'),
                'author_wx' => get_parameter('author_wx'),
                'ctime' => time(),
                'state' => 0,
            );
            $id = db_insert('article', $up_data);
            $article = article_get($id);
            $ret['status'] = 1;
            $ret['article_id'] = $id;
            $ret['info'] = '文章添加成功!';
        }
        json_out($ret);
    }
    function edit(){
        $ret = array( 'status' => 0, 'info' => 'Error', 'article_id' => 0 );
        $this->form_validation->set_rules ( 'article_id', '文章标题', 'trim|required|xss_clean');
        $this->form_validation->set_rules ( 'title', '文章标题', 'trim|required|xss_clean');
        $this->form_validation->set_rules ( 'cid', '文章分类', 'trim|required|xss_clean');
        $this->form_validation->set_rules ( 'content', '文章简介', 'trim|required|xss_clean');
        $this->form_validation->set_rules ( 'author', '文章作者', 'trim|required|xss_clean');

        $this->form_validation->set_rules ( 'code', '文章简介', 'trim|xss_clean');
        $this->form_validation->set_rules ( 'author_wx', '作者微信', 'trim|xss_clean');
        $this->form_validation->set_rules ( 'tid', '文章来源类型', 'trim|xss_clean');
        $this->form_validation->set_rules ( 'url', '文章链接', 'trim|xss_clean');
        $id = get_parameter('id', 0);
        $article = article_get($id);
        if ($article['state'] == 1) {
            if ($article['uid'] == $this->tk_user['id']) {
                $x_cid = [1 => 8, 2 => 9, 3 => 10, 4 => 11];
                $xcid_get = get_parameter('xcid', 0);
                $xcid = 0;
                if (in_array($xcid_get, $x_cid)) {
                    $xcid = $x_cid[$xcid_get];
                }
                if( $this->form_validation->run() ) {
                    $up_data = array(
                        'cid' => 1,
                        'xcid' => $xcid,
                        'tid' => get_parameter('tid'),
                        'title' => get_parameter('title', ''),
                        'content' => get_parameter('content', ''),
                        'code' => get_parameter('code', ''),
                        'url' => get_parameter('url', ''),
                        'author' => get_parameter('author'),
                        'author_wx' => get_parameter('author_wx'),
                        'ctime' => time(),
                        'state' => 0,
                    );
                    if($article){
                        article_update($article, $up_data);
                    }
                    $article = article_get($id);
                    $ret['status'] = 1;
                    $ret['article_id'] = $article['id'];
                    $ret['info'] = '文章修改成功!';
                }
            } else {
                $ret['info'] = '不能修改别人的文章!';
            }
        } else {
            $ret['info'] = '文章信息错误!';
        }
        json_out($ret);
    }
    function del(){
        $ret = array( 'status' => 0, 'info' => 'Error', 'article_id' => 0 );
        $id = get_parameter('id', 0);
        $article = article_get($id);
        if ($article) {
            if ($article['uid'] == $this->tk_user['id']) {
                $this->form_validation->set_rules ( 'id', '文章ID', 'trim|required|xss_clean');
                if( $this->form_validation->run() ) {
                    if($article){
                        article_update($article, ['state' => DEL_STATUS]);
                    }
                    $article = article_get($id);
                    $ret['status'] = 1;
                    $ret['info'] = '文章删除成功!';
                }
            } else {
                $ret['info'] = '不能删除别人的文章!';
            }
        }
        json_out($ret);
    }
}
